home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Communication / I-Spy scripts / Find different lines.pl < prev    next >
Perl Script  |  1995-12-23  |  2KB  |  72 lines

  1. #!/usr/bin/perl
  2.  
  3. #    Find different lines.pl
  4. #        This script finds lines that are not the same between two files
  5. #
  6. #        12/22/95 Version 1.0
  7. #        This script is free, public domain, no strings attached.
  8. #     Igor Livshits <igorl@uiuc.edu>
  9.  
  10.  
  11. $openToWrite=             "> ";
  12. $macPathDelimiter=     ":";
  13. $argumentCounter=        0;
  14.  
  15.  
  16. #    Read in the arguments passed to us from the AppleScript agent
  17. #
  18. $workingDirectory=  $ARGV[$argumentCounter++] . $macPathDelimiter;
  19. $firstFile=                  $ARGV[$argumentCounter++];
  20. $secondFile=                 $ARGV[$argumentCounter++];
  21. $linesAddedFile=         $ARGV[$argumentCounter++];
  22. $linesRetainedFile= "Lines retained";
  23. $linesRemovedFile=     "Lines removed";
  24.  
  25.  
  26. #    Set up input and output files
  27. #
  28. open(firstFile, $workingDirectory . $firstFile) || die "Cannot open $firstFile: $!";
  29. open(secondFile, $workingDirectory . $secondFile) || die "Cannot open $secondFile: $!";
  30. open(linesRetainedFile, $openToWrite . $workingDirectory . $linesRetainedFile) || die "Cannot open $linesRetainedFile: $!";
  31. open(linesAddedFile, $openToWrite . $workingDirectory . $linesAddedFile) || die "Cannot open $linesAddedFile: $!";
  32. open(linesRemovedFile, $openToWrite . $workingDirectory . $linesRemovedFile) || die "Cannot open $linesRemovedFile: $!";
  33.  
  34.  
  35. # Find the differences -- assumes sorted file contents
  36. #
  37. $currentSecondFileLine= <secondFile>;                                            # pre-fetch the next line of the second file
  38. while($currentFirstFileLine= <firstFile>)
  39. {
  40.     while (!eof(secondFile))
  41.     {                                                                                                                # skip until second file catches up with us
  42.         last if ($currentFirstFileLine le $currentSecondFileLine);
  43.         
  44.         print (linesAddedFile $currentSecondFileLine);                # this line is only in the second file
  45.         $currentSecondFileLine= <secondFile>;                                    # fetch the next line of the second file
  46.     }
  47.     
  48.     if ($currentFirstFileLine eq $currentSecondFileLine)
  49.     {
  50.         print (linesRetainedFile $currentFirstFileLine);            # here's a line common to both files
  51.         $currentSecondFileLine= <secondFile>;                                    # fetch the next line of the second file
  52.     }
  53.     else
  54.     {
  55.         print (linesRemovedFile $currentFirstFileLine);                # this line is only in the first file
  56.     }
  57. }
  58.  
  59. while ($currentSecondFileLine= <secondFile>)
  60. {                                                                                                                    # dump the residual unmatched lines
  61.     print (linesAddedFile $currentSecondFileLine);                    # this line is only in the second file
  62. }
  63.  
  64.  
  65. # Clean up
  66. #
  67. close(firstFile);
  68. close(secondFile);
  69. close(linesRetainedFile);
  70. close(linesAddedFile);
  71. close(linesRemovedFile);
  72.